<?php

function getPreviousNextPageText($Nbpages)
{
	
	$text['next_page_text'] = "";
	$text['previous_page_text'] = "";
		
	if (isset($_SESSION['page']))
	{
		
		if ($_SESSION['page'] > 1)
		{
			$text['previous_page_text'] = "	<form style='float:left; margin:0;'>
										<input type='hidden' name='page' value='".($_SESSION['page']-1)."'>";
										
			foreach ($_GET as $name => $value)
			{
				if ($name != "page")
				{
					$text['previous_page_text'] .= '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
				}
			}
										
			$text['previous_page_text'] .= "	<input type='submit' value ='Page ".($_SESSION['page']-1)."'>
									</form>";
		}
		if ($_SESSION['page'] < $Nbpages)
		{
			$text['next_page_text'] = "	<form style='float:right; margin:0;'>
									<input type='hidden' name='page' value='".($_SESSION['page']+1)."'>";
									
			foreach ($_GET as $name => $value)
			{
				if ($name != "page")
				{
					$text['next_page_text'] .= '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
				}
			}
			
			
			$text['next_page_text'] .= "	<input type='submit' value ='Page ".($_SESSION['page']+1)."'>
								</form>";
		}
	}
	else
	{
		if ($Nbpages > 1)
		{
			$text['next_page_text'] = "<form style='float:right; margin:0;'><input type='hidden' name='page' value='2'><input type='submit' value ='Page 2'>";
			
			foreach ($_GET as $name => $value)
			{
				if ($name != "page")
				{
					$text['next_page_text'] .= '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
				}
			}
			
			$text['next_page_text'] .= "</form>";
		}
	}

	return $text;

}

function newVisit()
{
	
	if (isset($_SERVER["HTTP_REFERER"]))
	{
		$last_path = explode($_SERVER["SERVER_NAME"],$_SERVER["HTTP_REFERER"]);
		$last_path = $last_path[1];
	}
	else
	{
		$last_path = "";
	}
	return $last_path != $_SERVER["PHP_SELF"];
	
}

function getActualPage()
{
	
	if (isset($_GET['page']) && isint($_GET['page']))
	{
		$_SESSION['page'] = $_GET['page'];
	}
	elseif (!isset($_SESSION['page']))
	{
		$_SESSION['page'] = 1;
	}
	
}

function getLocationUnderPromo($BDD_connexion,$page,$LocationsByPage)
{
	
	$nonOrderedLocations = array();
	$popularslocations = array();
	
	$request_count = "SELECT * FROM location,bien WHERE location.ref_bien = bien.id_bien and location.promo < location.prix GROUP BY location.ref_bien";
	$request_count .= " LIMIT ".($page*$LocationsByPage).",".((($page+1)*$LocationsByPage));
	$prepare=$BDD_connexion->prepare($request_count);
	$prepare->execute();
	while($location = $prepare->fetch(PDO::FETCH_OBJ))
	{
		$location->description = getLocationDescription($BDD_connexion,$location->id_location);
		$location->notation = getLocationNotation($BDD_connexion,$location->id_location);
		$nonOrderedLocations[] = $location;
	}
	
	$orderedLocations = orderLocationsByNotation($nonOrderedLocations);
	
	for ($i=0; $i < count($orderedLocations); $i++)
	{
		$popularslocations[$i] = $orderedLocations[$i];
	}

	return $popularslocations;

}

function orderLocationsByNotation($locations)
{
	
	$orderedLocations_temp = array();
	$locations_temp = array_values($locations);
	
	$size = count($locations_temp);
	$indexArray = 0;
	
	while ($indexArray < $size)
	{
		$maxIndex = getMaxNotationIndex($locations_temp);
		$orderedLocations_temp[$indexArray] = $locations_temp[$maxIndex];
		unset($locations_temp[$maxIndex]);
		$locations_temp = array_values($locations_temp);
		$indexArray++;
	}
	
	return $orderedLocations_temp;

}

function getMaxNotationIndex($locations)
{

	$maxIndex = 0;
	
	for ($i=0; $i < count($locations); $i++)
	{
		if ($locations[$i]->notation > $locations[$maxIndex]->notation)
		{
			$maxIndex = $i;
		}
	}
	
	return $maxIndex;
}

function getLocationPromoFieldset($location,$language)
{

	$return = 		'	<fieldset style="float:left; width:47%;">
					<legend>'.strtoupper($location->nom).' ('.$location->categorie.')</legend>
					<div style="float:left;text-align:center;">';
	
	$location_image = default_file_extension("Locations/".$location->id_bien."/default");
	if (file_exists($location_image))
	{
		$return .= '<img src="'.$location_image.'" class="img256rounded">';
	}
	else
	{
		$return .= '<img src="CSS/no_default_image.png" class="img256rounded">';
	}
					
	$return .= 		'	<form method="GET" action="location.php" style="padding-top:5px;">
					<input type="hidden" name="id" value="'.$location->id_bien.'" />
					<input type="submit" value=" '.$language[$_SESSION['lang']]["Accesslocation_button"].' "/>
					</form>
					</div>
					<div style="float:left; padding:10px;">'.$location->description.'</div>
					<div style="clear:left;float:right;"><span style="color:rgb('.(100-intval($location->promo/$location->prix*255)).','.(100-intval($location->promo/$location->prix*255)).',0)">- '.(100-intval($location->promo/$location->prix*100)).' %</span></div>
					</fieldset>';
					
	return $return;
	
}

function getTotalNbPages($BDD_connexion,$LocationsByPage)
{
	
	$countPg = 0;
	
	$count = "SELECT COUNT(*) AS count FROM location,bien WHERE location.ref_bien = bien.id_bien and location.promo < location.prix GROUP BY location.ref_bien";
		
	$preparecount = $BDD_connexion->prepare($count);
	$preparecount->execute();
	$nombre=$preparecount->fetch(PDO::FETCH_OBJ);
	
	if ($nombre != false)
	{
		$countPg = ($nombre->count - $nombre->count % $LocationsByPage) / $LocationsByPage;

		if ((($nombre->count % $LocationsByPage) > 0))
		{
			$countPg += 1;
		}
	}
	
	if ($countPg == 0)
	{
		$countPg++;
	}
	
	return $countPg;
	
}
?>
